package com.example.zhaohang.employmentapp;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.zhaohang.been.MesBeen;
import com.example.zhaohang.contect.ContectUtils;
import com.example.zhaohang.contect.UriContect;
import com.example.zhaohang.utils.GsonUtil;
import com.example.zhaohang.utils.ToastMake;
import com.example.zhaohang.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.MediaType;

public class ForgetActivity extends BasicActivity implements View.OnClickListener {


    private EditText  mName1;
    private Button    mName5;
    private EditText  mName3;
    private Button    mName8;
    private TimerTask wait;
    private int       time;

    private String mCode;
    private Timer timer = new Timer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_forget);
        mName1 = findViewById(R.id.name1);

        mName5 = findViewById(R.id.name5);
        mName8 = findViewById(R.id.name8);
        mName3 = findViewById(R.id.name3);
        setLeftShow(1, R.mipmap.back);
        getLift_V().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                finish();
            }
        });

        mName5.setOnClickListener(this);
        mName8.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            //点击发送验证码
            case R.id.name5:

                final String trim = mName1.getText().toString().trim();
                if (TextUtils.isEmpty(trim)) {
                    ToastMake.showToast(getResources().getString(R.string.name11));
                    return;
                }
                if (trim.length() != 11) {
                    ToastMake.showToast(getResources().getString(R.string.name12));
                    return;
                }
                if (!Utils.isMobileNO(trim)) {

                    return;
                }
                time = 60;
                wait = new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (time <= 0) {
                                    mName5.setEnabled(true);
                                    mName5.setFocusable(true);
                                    mName5.setFocusableInTouchMode(true);
                                    mName5.requestFocus();
                                    mName5.setText(getResources().getString(R.string.name19));
                                    mName1.setEnabled(true);
                                    wait.cancel();
                                } else {
                                    mName5.setEnabled(false);
                                    mName5.setFocusable(false);
                                    mName5.setFocusableInTouchMode(false);
                                    mName5.setText(time + "秒后重获");
                                    mName5.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                                }
                                time--;
                            }
                        });
                    }
                };
                timer.schedule(wait, 0, 1000);
                if (isConnected(ForgetActivity.this)) {
                    sendPostRequest(trim);
                } else {
                    ToastMake.showToast(getResources().getString(R.string.name17));
                    wait.cancel();
                    mName5.setEnabled(true);
                    mName5.setFocusable(true);
                    mName5.setFocusableInTouchMode(true);
                    mName5.requestFocus();
                    mName5.setText(getResources().getString(R.string.name19));
                    mName1.setEnabled(true);

                    return;
                }
                break;
            //点击注册
            case R.id.name8:
                if (isConnected(ForgetActivity.this)) {


                    nextPager();
                } else {
                    ToastMake.showToast(getResources().getString(R.string.name17));
                }
                break;
        }
    }

    private void nextPager() {


        String phoneNumber = mName1.getText().toString().trim();
        String code = mName3.getText().toString().trim();
        if (TextUtils.isEmpty(phoneNumber)) {
            ToastMake.showToast(getResources().getString(R.string.name11));
            return;
        }
        if (TextUtils.isEmpty(code)) {
            ToastMake.showToast(getResources().getString(R.string.name14));
            return;
        }

        if (!Utils.isMobileNO(phoneNumber)) {
            return;
        }


        if (!TextUtils.equals(code, mCode)) {
            ToastMake.showToast(getResources().getString(R.string.name15));
            return;
        }
        Intent intent = new Intent(ForgetActivity.this, ResetPasswordActivity.class);
        intent.putExtra(ContectUtils.FOGOTCODE, mCode);
        intent.putExtra(ContectUtils.FOGOPHONE, phoneNumber);
        startActivity(intent);


    }


    private void sendPostRequest(String phone) {
        HashMap<String, String> map = new HashMap<>();
        map.put("phone", phone);
        String json = GsonUtil.parseMapToJson(map);
        OkHttpUtils
                .postString()
                .url(UriContect.SMS)
                .content(json)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastMake.showToast(getResources().getString(R.string.name18));
                        mName5.setEnabled(true);
                        mName5.setFocusable(true);
                        mName5.setFocusableInTouchMode(true);
                        mName5.requestFocus();
                        mName5.setText(getResources().getString(R.string.name19));
                        mName1.setEnabled(true);
                        wait.cancel();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        MesBeen mesBeen = GsonUtil.parseJsonToBean(response, MesBeen.class);

                        if (mesBeen == null) {
                            ToastMake.showToast(getResources().getString(R.string.name18));
                            mName5.setEnabled(true);
                            mName5.setFocusable(true);
                            mName5.setFocusableInTouchMode(true);
                            mName5.requestFocus();
                            mName5.setText(getResources().getString(R.string.name19));
                            mName1.setEnabled(true);
                            wait.cancel();
                            return;
                        }
                        if (!TextUtils.equals(mesBeen.getStatus(), "1")) {
                            ToastMake.showToast(mesBeen.getMsg());
                            mName5.setEnabled(true);
                            mName5.setFocusable(true);
                            mName5.setFocusableInTouchMode(true);
                            mName5.requestFocus();
                            mName5.setText(getResources().getString(R.string.name19));
                            mName1.setEnabled(true);
                            wait.cancel();
                            return;
                        } else {
                            mCode = mesBeen.getCode();
                        }
                    }
                });

    }

}
